package com.prs.crm.service.crm.impl;

import java.io.File;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Collection;
import java.util.List;

import org.courser.Util;

import com.prs.crm.context.Attach;
import com.prs.crm.context.Constrains;
import com.prs.crm.context.FileUtil;
import com.prs.crm.dao.crm.LinkmanDao;
import com.prs.crm.domain.crm.Customer;
import com.prs.crm.domain.crm.CustomerAgent;
import com.prs.crm.domain.crm.CustomerSupplier;
import com.prs.crm.domain.crm.Linkman;
import com.prs.crm.service.crm.LinkmanService;
import com.prs.crm.service.impl.BaseServiceImpl;

public class LinkmanServiceImpl extends BaseServiceImpl<Linkman, LinkmanDao>
		implements LinkmanService {

	@Override
	public void saveLinkman(Linkman linkman, File file, String fileName) {
		// TODO Auto-generated method stub
		linkman.setAttach(FileUtil.getInstance().createAttach(file, fileName));
		this.getDao().save(linkman);
	}

	@Override
	public List<Linkman> getLinkmanByCustomer(Customer customer) {
		// TODO Auto-generated method stub
		return this.getDao().getLinkmanByCustomer(customer);
	}

	@Override
	public Collection<Linkman> getAllLinkman() {
		// TODO Auto-generated method stub
		return this.getDao().getAllLinkman();
	}

	@Override
	public Collection<Linkman> getTodayBirthdayLinkman() {
		// TODO Auto-generated method stub
		Collection<Linkman> results = new ArrayList<Linkman>();
		Collection<Linkman> linkmans = this.getDao().getAllLinkman();
		Calendar todayCalendar = Calendar.getInstance();

		for (Linkman linkman : linkmans) {
			if (linkman.getBirthday() != null) {
				Calendar linkmanCalendar = Calendar.getInstance();
				linkmanCalendar.setTime(linkman.getBirthday());

				if ((todayCalendar.get(Calendar.MONTH) == linkmanCalendar.get(Calendar.MONTH))
						&& (todayCalendar.get(Calendar.DAY_OF_MONTH) == linkmanCalendar.get(Calendar.DAY_OF_MONTH))) {
					results.add(linkman);
				}
			}
		}
		return results;
	}

	@Override
	public void saveLinkmans(Linkman linkman, File file, String fileName) {
		// TODO Auto-generated method stub
		Attach pAttach = FileUtil.getInstance().saveFile(file,
				fileName, "", Constrains.UPLOAD_DIR_LINKMAN);
		
		if (pAttach.getSrc() != null) {
			linkman.setLinkman_file(pAttach.getSrc());
		}

		this.getDao().save(linkman);
	}

	@Override
	public List<Linkman> getLinkmanByCustomerAgent(CustomerAgent customerAgent) {
		// TODO Auto-generated method stub
		return this.getDao().getLinkmanByCustomerAgent(customerAgent);
	}

	@Override
	public List<Linkman> getLinkmanByCustomerSupplier(
			CustomerSupplier customerSupplier) {
		// TODO Auto-generated method stub
		return this.getDao().getLinkmanByCustomerSupplier(customerSupplier);
	}

}
